package com.springapi.controller;

import com.springapi.entity.Setting;
import com.springapi.entity.Shop;
import com.springapi.service.SettingService;
import com.springapi.service.ShopService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应用明细(Shop)表控制层
 *
 * @author makejava
 * @since 2025-01-22 13:54:34
 */
@RestController
@RequestMapping("/api/shop")
public class ShopController {
    /**
     * 服务对象
     */
    @Resource
    private ShopService shopService;


    @Resource
    private SettingService settingService;

    /**
     * 分页查询
     *
     * @param shop 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Shop>> queryByPage(@RequestBody Shop shop, PageRequest pageRequest) {
        return ResponseEntity.ok(this.shopService.queryByPage(shop, pageRequest));
    }

    //商户短信充值
    @GetMapping("/updateshopdxnumber")
    public ResponseEntity<Shop> updateshopdxnumber(@RequestParam Integer shopid, @RequestParam Integer xnumber) {
        Shop res=new Shop();

        Shop s=this.shopService.queryById(shopid);
        s.setDxnumber(s.getDxnumber()+xnumber);
         res=this.shopService.update(s);

        return ResponseEntity.ok(res);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryById")
    public ResponseEntity<Shop> queryById(@RequestParam int id) {
        return ResponseEntity.ok(this.shopService.queryById(id));
    }
    /**
     * 新增数据
     *
     * @param shop 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Shop> add(@RequestBody Shop shop) {
        List<Shop> shopList=this.shopService.findbyshopname(shop.getName());
        Shop shop2=new Shop();
        if(shopList.size()>0) {
            shop2.setName("商户已存在");
        }else{
            try {
                shop2=this.shopService.insert(shop);


                Setting setting=new Setting();
                setting.setName("是否发送短信");
                setting.setValue("否");
                setting.setIsdelete("否");
                setting.setAppid(1);
                setting.setShopid(shop2.getId());
                settingService.insert(setting);

                Setting setting2=new Setting();
                setting2.setName("是否打印标签");
                setting2.setValue("否");
                setting2.setIsdelete("否");
                setting2.setAppid(1);
                setting2.setShopid(shop2.getId());
                settingService.insert(setting2);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }


        }
        return ResponseEntity.ok(shop2);
    }

    /**
     * 编辑数据
     *
     * @param shop 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Shop> edit(@RequestBody Shop shop) {

        return ResponseEntity.ok(this.shopService.update(shop));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.shopService.deleteById(id));
    }

}

